# -*- coding: utf-8 -*-
from db.session import with_session
from db.models.attributes import (
    ApplicableObject, KnowledgeType, ApplicableArea, ElementClassification, AccidentType
)


# Generic helpers
@with_session
def db_list_by_model(session, model):
    return session.query(model).all()


@with_session
def db_get_by_id(session, model, obj_id: int):
    return session.query(model).filter(model.id == obj_id).first()


@with_session
def db_create_with_name(session, model, name: str):
    obj = model(name=name)
    session.add(obj)
    session.commit()
    return obj


@with_session
def db_update_name(session, model, obj_id: int, name: str):
    obj = session.query(model).filter(model.id == obj_id).first()
    if obj:
        obj.name = name
        session.commit()
        return obj
    return None


@with_session
def db_delete_by_id(session, model, obj_id: int):
    obj = session.query(model).filter(model.id == obj_id).first()
    if obj:
        session.delete(obj)
        session.commit()
        return True
    return False


# Concrete wrappers
def db_get_all_applicable_objects():
    return db_list_by_model(ApplicableObject)


def db_get_applicable_object_by_id(obj_id: int):
    return db_get_by_id(ApplicableObject, obj_id)


def db_create_applicable_object(name: str):
    return db_create_with_name(ApplicableObject, name)


def db_update_applicable_object(obj_id: int, name: str):
    return db_update_name(ApplicableObject, obj_id, name)


def db_delete_applicable_object(obj_id: int):
    return db_delete_by_id(ApplicableObject, obj_id)


def db_get_all_knowledge_types():
    return db_list_by_model(KnowledgeType)


def db_get_knowledge_type_by_id(type_id: int):
    return db_get_by_id(KnowledgeType, type_id)


def db_create_knowledge_type(name: str):
    return db_create_with_name(KnowledgeType, name)


def db_update_knowledge_type(type_id: int, name: str):
    return db_update_name(KnowledgeType, type_id, name)


def db_delete_knowledge_type(type_id: int):
    return db_delete_by_id(KnowledgeType, type_id)


def db_get_all_applicable_areas():
    return db_list_by_model(ApplicableArea)


def db_get_applicable_area_by_id(area_id: int):
    return db_get_by_id(ApplicableArea, area_id)


def db_create_applicable_area(name: str):
    return db_create_with_name(ApplicableArea, name)


def db_update_applicable_area(area_id: int, name: str):
    return db_update_name(ApplicableArea, area_id, name)


def db_delete_applicable_area(area_id: int):
    return db_delete_by_id(ApplicableArea, area_id)


def db_get_all_element_classifications():
    return db_list_by_model(ElementClassification)


def db_get_element_classification_by_id(classification_id: int):
    return db_get_by_id(ElementClassification, classification_id)


@with_session
def db_get_element_classification_by_name(session, name: str):
    """根据名称查找要素分类，如果不存在则创建"""
    classification = session.query(ElementClassification).filter(ElementClassification.name == name).first()
    if not classification:
        return None
    return classification


def db_create_element_classification(name: str):
    return db_create_with_name(ElementClassification, name)


def db_update_element_classification(classification_id: int, name: str):
    return db_update_name(ElementClassification, classification_id, name)


def db_delete_element_classification(classification_id: int):
    return db_delete_by_id(ElementClassification, classification_id)


# AccidentType
def db_get_all_accident_types():
    return db_list_by_model(AccidentType)


def db_get_accident_type_by_id(type_id: int):
    return db_get_by_id(AccidentType, type_id)


def db_create_accident_type(name: str):
    return db_create_with_name(AccidentType, name)


def db_update_accident_type(type_id: int, name: str):
    return db_update_name(AccidentType, type_id, name)


def db_delete_accident_type(type_id: int):
    return db_delete_by_id(AccidentType, type_id)


